home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / viewers / polyview / polyvw31.lha / Polyview3.1 / new / pvutil.c < prev    next >
C/C++ Source or Header  |  1993-06-23  |  9KB  |  310 lines

  1. /*****************************************************************************
  2.  * NCSA Polyview 3.0                                                         *
  3.  *                                                                           *
  4.  * Version 3 changes and additions by Marc Andreessen.                       *
  5.  * Version 2 by Brian Calvert.                                               *
  6.  *                                                                           *
  7.  * Software Development Group                                                *
  8.  * National Center for Supercomputing Applications                           *
  9.  * University of Illinois at Urbana-Champaign                                *
  10.  *                                                                           *
  11.  * This is BETA release software.  As such it may contain software bugs and  *
  12.  * exhibit inconsistencies.                                                  *
  13.  *                                                                           *
  14.  * Please send bug reports to polyview@ncsa.uiuc.edu.                        *
  15.  *                                                                           *
  16.  * Copyright (c) 1992 The Board of Trustees of the University of Illinois.   *
  17.  *                                                                           *
  18.  * Permission to use, copy, and modify this software and its                 *
  19.  * documentation for educational, research, and non-profit purposes is       *
  20.  * hereby granted, provided that the above copyright notice, the original    *
  21.  * authors names, and this permission notice appear in all such copies.      *
  22.  * Any distribution of this software requires the explicit and written       *
  23.  * authorization of the authors.                                             *
  24.  *                                                                           *
  25.  * The University of Illinois makes no representations about the             *
  26.  * suitability of this software for any purpose.  It is provided "as is"     *
  27.  * without warranty of any kind.                                             *
  28.  *****************************************************************************/
  29.  
  30. /* $Header: /usr3/people/gbourhis/pv3/new/RCS/pvutil.c,v 1.1 92/09/18 10:55:26 marca Exp $ */
  31.  
  32. #ifdef RCSLOG
  33. $Log:    pvutil.c,v $
  34.  * Revision 1.1  92/09/18  10:55:26  marca
  35.  * Initial revision
  36.  * 
  37. #endif
  38.  
  39. #include "pv.h"
  40.  
  41. int bprintf(state_t * state, char * format, ...)
  42. {
  43.   va_list args;
  44.   char line[MAXLINELEN];
  45.   
  46.   /* Initialize args to the beginning of the list of arguments.  Get */
  47.   /* the state record pointer and set format to point to the first */
  48.   /* string in the list (the format string). */
  49.   va_start(args, format);
  50.   
  51.   /* Print the formatted string and arguments to the status window. */
  52.   vsprintf(line, format, args);
  53.   GUIsetStatus (line);
  54.   
  55.   /* Print the formatted string and arguments to the transcript, if */
  56.   /* there is a transcript file. */
  57.   if (state->transcript_fp != NULL) 
  58.     {
  59.       fprintf(state->transcript_fp, "; ");
  60.       if (vfprintf(state->transcript_fp, format, args) < 0) 
  61.         {
  62.           printf("SYSTEM ERROR:  Transcript vprintf in bprintf.\n");
  63.           fflush (stdout);
  64.           return ST_ERROR;
  65.         }
  66.     }
  67.   
  68.   /* Shut down the arguments list. */
  69.   va_end(args);
  70.   return ST_OKAY;
  71. }
  72.  
  73.  
  74. int gprintf(state_t * state, int x, int y, int attr, char * format, ...)
  75. {
  76.   va_list args;
  77.   char line[MAXLINELEN];
  78.   int strw, strh;
  79.   
  80.   /* Initialize args to the beginning of the list of arguments.  Get */
  81.   /* the state record pointer and set format to point to the first */
  82.   /* string in the list (the format string). */
  83.   va_start(args, format);
  84.   
  85.   /* Print the formatted string and arguments to the array. */
  86.   if (vsprintf(line, format, args) < 0) 
  87.     {
  88.       printf("SYSTEM ERROR:  vsprintf in gprintf.\n");
  89.       return ST_ERROR;
  90.     }
  91.     
  92.   /* Print the formatted string and arguments to the graphics window */
  93.   /* using the formatting flags. */
  94.   strw = strwidth(line);
  95.   strh = getheight();
  96.   
  97.   /* GPR_LEFTJ is the default. */
  98.   /* GPR_RIGHTJ means that the string should _end_ at (x,y). */
  99.   if (attr & GPR_RIGHTJ)
  100.     x -= (strw-1);
  101.   /* GPR_CENTERH means that the center of the string should be at x. */
  102.   else if (attr & GPR_CENTERH)
  103.     x -= strw/2;
  104.   
  105.   /* GPR_BOTTOMJ is the default */
  106.   /* GPR_TOPJ means that the top of the string should be at y. */
  107.   if (attr & GPR_TOPJ)
  108.     y -= (strh-1);
  109.   /* GPR_CENTERV means that the vertical center of the string should */
  110.   /* be at y. */
  111.   else if (attr & GPR_CENTERV)
  112.     y -= (strh/2 - 1);
  113.   
  114.   /* Move to the appropriate location and write the string. */
  115.   cmov2(x, y);
  116.   charstr(line);
  117.   
  118.   /* Underline the text, if requested. */
  119.   if (attr & GPR_UNDERL) 
  120.     {
  121.       bgnline();
  122.       p2f(x,y-2);
  123.       p2f(x+strw, y-2);
  124.       endline();
  125.     }
  126.   
  127.   /* Shut down the arguments list. */
  128.   va_end(args);
  129.   return ST_OKAY;
  130. }
  131.  
  132.  
  133. int stprintf(state_t * state, char * format, ...)
  134. /* DONE */
  135. /* Prints a string to the status window. */
  136. {
  137.   char line[MAXLINELEN];
  138.   va_list args;
  139.   
  140.   /* Initialize args to the beginning of the list of arguments.  Get */
  141.   /* the state record pointer and set format to point to the first */
  142.   /* string in the list (the format string). */
  143.   va_start(args, format);
  144.   
  145.   /* If there is no status window, print the message to stdout. */
  146.   if (state->status == NULL) 
  147.     {
  148.       /* Print the formatted string and arguments to stdout. */
  149.       bprintf(state, format, args);
  150.     }
  151.   else 
  152.     {
  153.       /* Copy the string to the status window. */
  154.       vsprintf(line, format, args);
  155.       GUIsetStatus (line);
  156.     }
  157.   
  158.   /* Shut down the arguments list. */
  159.   va_end(args);
  160.   
  161.   return ST_OKAY;
  162. }
  163.  
  164.  
  165. /* Copies "source" to "dest", substituting the value of "new" for all */
  166. /* occurrences of the  value "old".  Assumes sizeof(dest) > strlen(source)- */
  167. /* strlen(old)+strlen(new).  Always returns the value of dest. */
  168. char *strsub(char *dest, char *source, char *old, char *new)
  169. {
  170.   char *start, *next;
  171.     
  172.   /* Initialize the pointers to the beginning of their buffers. */
  173.   start = source;
  174.   *dest = '\0';
  175.   
  176.   /* Keep copying as long as there are matches. */
  177.   while (next = strstr(start, old)) 
  178.     {
  179.       /* Copy the part of the source that was skipped over. */
  180.       strncat(dest, start, (size_t) (next-start));
  181.       
  182.       /* Substitute the new string. */
  183.       strcat(dest, new);
  184.       
  185.       /* Change the starting point of our search. */
  186.       start = next + strlen(old);
  187.     }
  188.   
  189.   /* Append the rest of the source. */
  190.   strcat(dest, start);
  191.   
  192.   return dest;
  193. }
  194.  
  195.  
  196. char *substitute(char *dest, char *source, char *pattern, char *format, ...)
  197. {
  198.   char line[MAXLINELEN];
  199.   va_list args;
  200.   
  201.   /* Initialize args to the beginning of the list of arguments.  Get */
  202.   /* the state record pointer and set format to point to the first */
  203.   /* string in the list (the format string). */
  204.   va_start(args, format);
  205.   
  206.   /* Print the formatted string and arguments to stdout. */
  207.   if (vsprintf(line, format, args) < 0) 
  208.     {
  209.       printf("SYSTEM ERROR:  vsprintf in strsub.\n");
  210.       return dest;
  211.     }
  212.   
  213.   /* Substitute the formatted string into the source and return it */
  214.   /* in dest. */
  215.   strsub(dest, source, pattern, line);
  216.   
  217.   /* Shut down the arguments list. */
  218.   va_end(args);
  219.   return dest;
  220. }
  221.  
  222.  
  223. int strwcmp(char *wildstr, char *str)
  224. {
  225.   while ( (*wildstr != '\0') && (*str != '\0') && (*wildstr == *str) ) 
  226.     {
  227.       wildstr++;
  228.       str++;
  229.     }
  230.   
  231.   if (*wildstr == '*')
  232.     return 0;
  233.   else
  234.     return (*wildstr - *str);
  235. }
  236.  
  237.  
  238.  
  239. /* COORDINATE CONVERSION ROUTINES: */
  240. float facos2(float x, float r)
  241. {
  242.   return facos(x/r);
  243. }
  244.  
  245.  
  246. float fasin2(float y, float r)
  247. {
  248.   return (fasin(y/r));
  249. }
  250.  
  251.  
  252. void cart_to_sphere(float x, float y, float z, float *rho, 
  253.                     float *theta, float *phi)
  254. {
  255.   *rho = fhypot(x, fhypot(y, z));
  256.   *theta = fatan2(y, x);
  257.   *phi = facos2(z, *rho);
  258.  
  259.   return;
  260. }
  261.  
  262.  
  263. void sphere_to_cart(float rho, float theta, float phi, float *x, float *y, 
  264.                     float *z)
  265. {
  266.   *x = rho * fcos(theta) * fsin(phi);
  267.   *y = rho * fsin(theta) * fsin(phi);
  268.   *z = rho * fcos(phi);
  269.  
  270.   return;
  271. }
  272.  
  273.  
  274. /* Returns a pointer to an array composed of the coordinates. */
  275. float *p3f (float x, float y, float z)
  276. {
  277.   float vertex[3];
  278.   
  279.   vertex[0] = x;
  280.   vertex[1] = y;
  281.   vertex[2] = z;
  282.   
  283.   return vertex;
  284. }
  285.  
  286. void p2i(long x, long y)
  287. {
  288.   long vector[2];
  289.   
  290.   /* Copy the coordinates into an array so that we can call v2i. */
  291.   vector[0] = x;
  292.   vector[1] = y;
  293.   
  294.   /* Tell the graphics pipeline to use this vertex. */
  295.   v2i(vector);
  296. }
  297.  
  298.  
  299. void p2f(float x, float y)
  300. {
  301.   float    vector[2];
  302.   
  303.   /* Copy the coordinates into an array so that we can call v2i. */
  304.   vector[0] = x;
  305.   vector[1] = y;
  306.   
  307.   /* Tell the graphics pipeline to use this vertex. */
  308.   v2f(vector);
  309. }
  310.